package product;

import java.util.ArrayList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Test1 {

	/**统计没有农产品市场的省份
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		if (args.length!=3) {
			System.err.println("path err");
			System.exit(-1);
		}
		Job job=new Job(new Configuration(),"no");
		job.setJarByClass(Test1.class);
		
		MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class,allMaps.class);
		MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, someMaps.class);
		
		FileOutputFormat.setOutputPath(job, new Path(args[2]));
		job.setReducerClass(noReduces.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.waitForCompletion(true);
	}
	public static class allMaps extends Mapper<LongWritable, Text,Text,Text>{
		public static final String LABEL="w_";
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String lines = value.toString();
			context.write(new Text(lines),new Text(LABEL+lines));
		};
	}
	public static class someMaps extends Mapper<LongWritable, Text,Text,Text>{
		public static final String RIGHT="s_";
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split("\t");
			if (lines.length==6) {
				String provice = lines[4].trim();
				context.write(new Text(provice), new Text(RIGHT+provice));
			}
		};
	}
	//这两个map输出的结果都需要shuffle阶段的处理，处理后的数据变成以下形式：
	//<shanxi,{w_shanxi,s_shanxi}
	//<xianggang,{s_xianggan}
	public static class noReduces extends Reducer<Text, Text,Text,Text>{
		protected void reduce(Text k2, java.lang.Iterable<Text> values, org.apache.hadoop.mapreduce.Reducer<Text,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String prov=null;
			ArrayList<String> list=new ArrayList<String>();
			for (Text str : values) {
				String info = str.toString();
				if (info.startsWith(allMaps.LABEL)) {
					list.add(info.substring(2));
				}else if(info.startsWith(someMaps.RIGHT)){
					prov=info.substring(2);
				}
			}
			if (prov==null && list.size()>0) {
				for (String provice : list) {
					context.write(new Text(provice),new Text());
				}
			}
		};
	}

}
